<template>
  <el-row>
    <el-col>
      <el-form :inline="true" :model="resourceFilter" @keyup.enter.native="getDataList()" size="small">
        <el-form-item label="服务名称">
          <el-input v-model="resourceFilter.resourceName" style="width: 215px" placeholder="服务名称"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
              v-model="resourceFilter.useTimeRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
        <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="resourceName"
            header-align="center"
            align="center"
            label="服务名称">
        </el-table-column>
        <el-table-column
            prop="resourceDescribe"
            header-align="center"
            align="center"
            label="服务描述">
        </el-table-column>
        <el-table-column
            prop="isStartup"
            header-align="center"
            align="center"
            label="开启/关闭">
          <template #default="scope">
            <el-switch
                @change="updatedState(scope.row)"
                v-model="scope.row.isStartup"
                active-value="1"
                inactive-value="0">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            prop="visitRestriction"
            header-align="center"
            align="center"
            label="开放限制"
            :formatter="showServLimit">
        </el-table-column>
        <el-table-column
            prop="servKeyword"
            header-align="center"
            align="center"
            label="关键词">
        </el-table-column>
        <el-table-column
            prop="createdTime"
            header-align="center"
            align="center"
            label="发布时间">
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          :total="page.total"
          layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-col>
  </el-row>
</template>

<script>
import pickerShortcut from "@/mixins/picker-shortcut";
import {queryResourceItemPage, updatedResourceItemState} from "@/api/resources-center/service";

export default {
  name: "ApplicationServiceList",
  mixins: [pickerShortcut],
  data() {
    return {
      resourceFilter: {
        resourceName: '',
        resourceType: '2',
        useTimeRange: []
      },
      dataList: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      serviceUpdatedVisible: false
    };
  },
  created() {
    this.getDataList()
  },
  methods: {
    /**
     * {获取资源列表}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    getDataList() {
      this._debounce(() => queryResourceItemPage(this.page.currentPage, this.page.pageSize, this.resourceFilter).then(res => {
        if (res.status) {
          const data = res.data;
          this.dataList = data.records;
          this.page.total = data.total;
        }
      }), 500);
    },
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },

    /**
     * {更新资源状态}
     *
     * @author: 宋康
     * @date: 2023/2/15
     */
    updatedState(row) {
      updatedResourceItemState(row.resourceId, row.isStartup).then(res => {
        if (res.status) {
          this._success("修改完成")
          this.getDataList();
        } else {
          this._error(res.msg)
        }
      })
    },
    /**
     * 创建服务
     */
    createService() {
      this.$router.push({
        name: "MapServiceCreate",
      });
    },
    showServLimit(row, column, cellValue, index) {
      if (cellValue == 1) {
        return '可见'
      }
      if (cellValue == 2) {
        return '条件可见'
      }
      if (cellValue == 3) {
        return '线下'
      }
    }
  }
};
</script>

<style scoped>

</style>
